﻿/*----------------------------------------------------------------
// Copyright (C) 2009 
// 版权所有。 
//
// 文件名：
// 文件功能描述：固定资产信息管理
//
//
// 创建标识：
// 程序作者：翟允锋
// 修改标识：
// 修改描述：
//----------------------------------------------------------------*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;
using Wuqi.Webdiyer;

public partial class Equipment : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaEquip dalEquip = new DaEquip();
    private DaTfile dalTfile = new DaTfile();
    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            AspNetPager.PageSize = 15;

            this.BtnQuery_Click(null, null);

            this.SetPanelVisiable(SummaryPanel);

            if (Request.QueryString["deviceid"] != null && !string.IsNullOrEmpty(Request.QueryString["deviceid"].ToString()))
            {
                if (Request.QueryString["mode"] != null && !string.IsNullOrEmpty(Request.QueryString["mode"].ToString()) && Request.QueryString["mode"].ToString().Equals("edit"))
                {
                    this.DisplayEditInfo(Request.QueryString["deviceid"].ToString());
                    this.SetPanelVisiable(EditPanel);
                }
            }
        }
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        this.BtnQuery_Click(null, null);
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        try
        {
            DateTime.Parse(EditEx_Fact_Date.Text.Trim());
            DateTime.Parse(EditBuy_Date.Text.Trim());
        }
        catch
        {
            OperationResult.Text = "请输入正确的日期格式！"; divResult.Visible = true;
            return;
        }

        if (string.IsNullOrEmpty(EditUseful_Life.Text.Trim()))
        {
            OperationResult.Text = "请输入设备的有效期！"; divResult.Visible = true;
            return;
        }

        if (string.IsNullOrEmpty(EditMaintain_Day_Cyc.Text.Trim()))
        {
            OperationResult.Text = "请输入设备的维修周期！"; divResult.Visible = true;
            return;
        }

        MoEquip mod = new MoEquip();
        try
        {
            mod.Equip_Id = EditEquip_ID.Text;

            mod.Equip_Name = EditEquip_Name.Text.Trim();
            mod.Oem = EditOEM.Text.Trim();
            mod.Ex_Fact_Date = DateTime.Parse(EditEx_Fact_Date.Text.Trim());
            //mod.Nameplate = EditNameplate.Text.Trim();
            mod.Buy_Date = DateTime.Parse(EditBuy_Date.Text.Trim());
            mod.Useful_Life = decimal.Parse(string.IsNullOrEmpty(EditUseful_Life.Text.Trim()) ? "0" : EditUseful_Life.Text.Trim());
            mod.Maintain_Day_Cyc = int.Parse(string.IsNullOrEmpty(EditMaintain_Day_Cyc.Text.Trim()) ? "0" : EditMaintain_Day_Cyc.Text.Trim());
            mod.Equip_Configs = EditEquip_Configs.Text.Trim();
            int rtn = dalEquip.Update(mod);
            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "设备信息修改成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "设备信息修改失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("设备信息修改失败", ex);
            OperationResult.Text = "设备信息修改失败！"; divResult.Visible = true;
        }
    }

    protected void EditBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        try
        {
            DateTime.Parse(CreateEx_Fact_Date.Text.Trim());
            DateTime.Parse(CreateBuy_Date.Text.Trim());
        }
        catch
        {
            OperationResult.Text = "请输入正确的日期格式！"; divResult.Visible = true;
            return;
        }

        if(string.IsNullOrEmpty(CreateUseful_Life.Text.Trim()))
        {
            OperationResult.Text = "请输入设备的有效期！"; divResult.Visible = true;
            return;
        }

        if(string.IsNullOrEmpty(CreateMaintain_Day_Cyc.Text.Trim()))
        {
            OperationResult.Text = "请输入设备的维修周期！"; divResult.Visible = true;
            return;
        }

        MoEquip mod = new MoEquip();

        try
        {
            mod.Equip_Id = SequenceKeys.GenerateEquipID();
            mod.Equip_Name = CreateEquip_Name.Text.Trim();
            mod.Oem = CreateOEM.Text.Trim();
            mod.Ex_Fact_Date = DateTime.Parse( CreateEx_Fact_Date.Text.Trim());
            //mod.Nameplate = CreateNameplate.Text.Trim();
            mod.Buy_Date = DateTime.Parse( CreateBuy_Date.Text.Trim());
            mod.Useful_Life = decimal.Parse(string.IsNullOrEmpty(CreateUseful_Life.Text.Trim()) ? "0" : CreateUseful_Life.Text.Trim());
            mod.Maintain_Day_Cyc = int.Parse(string.IsNullOrEmpty(CreateMaintain_Day_Cyc.Text.Trim()) ? "0" : CreateMaintain_Day_Cyc.Text.Trim());
            mod.Equip_Configs = CreateEquip_Configs.Text.Trim();

            int rtn = dalEquip.Add(mod);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "设备信息创建成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "设备信息创建失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("设备信息创建失败", ex);
            OperationResult.Text = "设备信息创建失败！"; divResult.Visible = true;
        }
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void DetailBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void EditGo_Click(object sender, EventArgs e)
    {
        string strKey = DetailEquip_ID.Text;
        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void DeleteGo_Click(object sender, EventArgs e)
    {
        try
        {
            string strKey = DetailEquip_ID.Text;
            int rtn = this.DeleteInfo(strKey);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "设备信息删除成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "设备信息删除失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error("设备信息删除失败", ex);
            OperationResult.Text = "设备信息删除失败！"; divResult.Visible = true;
        }

    }

    protected void GvDelete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = ((GridView)gvr.NamingContainer).DataKeys[gvr.RowIndex].Value.ToString();

        this.DeleteInfo(strKey);
        this.BtnQuery_Click(null, null);
    }

    protected void GvEdit_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = ((GridView)gvr.NamingContainer).DataKeys[gvr.RowIndex].Value.ToString();

        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void GvDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = ((GridView)gvr.NamingContainer).DataKeys[gvr.RowIndex].Value.ToString();

        this.DisplayDetailInfo(strKey);
        this.SetPanelVisiable(DetailPanel);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        EditPanel.Visible = false;
        DetailPanel.Visible = false;
        CreatePanel.Visible = false;
        SummaryPanel.Visible = false;

        panel.Visible = true;
    }

    protected void BtnQuery_Click(object sender, EventArgs e)
    {
        //这里查询设备信息并绑定到gridview上。
        int pagecount = 0;
        string strCondition = "where 1=1 ";
        if (sender != null && e != null)
        {
            AspNetPager.CurrentPageIndex = 0;
        }

        if (!string.IsNullOrEmpty(TxtEquipID.Text.Trim()))
        {
            strCondition = strCondition + "and  Equip_ID='" + TxtEquipID.Text.Trim() + "'";
        }
        //if (!string.IsNullOrEmpty(TxtEmpID.Text.Trim()))
        //{
        //    strCondition = strCondition + "and  Emp_ID='" + TxtEmpID.Text.Trim() + "'";
        //}
        this.GridView.DataSource = dalEquip.GetEquipmentRecords(strCondition, AspNetPager.PageSize, AspNetPager.CurrentPageIndex - 1, out pagecount);

        GridView.DataBind();

        AspNetPager.RecordCount = pagecount;

        this.SetPanelVisiable(SummaryPanel);
    }

    protected int DeleteInfo(string strKey)
    {
        //执行删除操作。
        return dalEquip.Delete(strKey);
    }

    protected void DisplayDetailInfo(string strKey)
    {
        //这里查询设备信息
        MoEquip mod = dalEquip.GetEntity(strKey);

        //然后给查看视图赋值。
        DetailEquip_ID.Text = mod.Equip_Id;
        DetailEquip_Name.Text = mod.Equip_Name;
        DetailOEM.Text = mod.Oem;
        DetailEx_Fact_Date.Text = mod.Ex_Fact_Date.ToString("yyyy-MM-dd");
        DetailNameplate.Text = mod.Nameplate;
        DetailBuy_Date.Text = mod.Buy_Date.ToString("yyyy-MM-dd");
        DetailUseful_Life.Text = mod.Useful_Life.ToString("F0");
        DetailMaintain_Day_Cyc.Text = mod.Maintain_Day_Cyc.ToString();
        DetailEquip_Configs.Text = mod.Equip_Configs;


        DetailRepFile.DataSource = dalTfile.QueryFileRelation(strKey);
        DetailRepFile.DataBind();

    }

    protected void DisplayEditInfo(string strKey)
    {
        //这里查询设备信息
        MoEquip mod = dalEquip.GetEntity(strKey);

        //然后给编辑视图赋值。
        EditEquip_ID.Text = mod.Equip_Id;
        EditEquip_Name.Text = mod.Equip_Name;
        EditOEM.Text = mod.Oem;
        EditEx_Fact_Date.Text = mod.Ex_Fact_Date.ToString("yyyy-MM-dd");
        //EditNameplate.Text = mod.Nameplate;
        EditBuy_Date.Text = mod.Buy_Date.ToString("yyyy-MM-dd");
        EditUseful_Life.Text = mod.Useful_Life.ToString("F0");
        EditMaintain_Day_Cyc.Text = mod.Maintain_Day_Cyc.ToString();
        EditEquip_Configs.Text = mod.Equip_Configs;

        EditRepFile.DataSource = dalTfile.QueryFileRelation(strKey);
        EditRepFile.DataBind();
    }

    protected void CreateCancel()
    {
        CreateEquip_ID.Text = "";
        CreateEquip_Name.Text = "";
        CreateOEM.Text = "";
        CreateEx_Fact_Date.Text = "";
        //CreateNameplate.Text = "";
        CreateBuy_Date.Text = "";
        CreateUseful_Life.Text = "";
        CreateMaintain_Day_Cyc.Text = "";
        CreateEquip_Configs.Text = "";
    }
    protected void btnAddAttachment_Click(object sender, EventArgs e)
    {
        Response.Redirect("UploadFiles.aspx?nameplate&deviceid=" + EditEquip_ID.Text.Trim());
    }
    protected void RepFile_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strKey = e.CommandName;
        try
        {
            dalTfile.DeleteOneAttachFile(strKey);
            this.DisplayEditInfo(EditEquip_ID.Text);
            OperationResult.Text = "删除附件成功!"; divResult.Visible = true;
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "删除附件失败!"; divResult.Visible = true;
        }
    }
}
